#include "time_count.h"
#include <stdio.h>
#include <stdlib.h>

void tm_start(struct timespec *tp)
{
	tp->tv_sec = 0;
	tp->tv_nsec = 0;
	if (clock_gettime(CLOCK_MONOTONIC, tp) != 0)
	{
		perror("clock_gettime tm_start failed");
		exit(-1);
	}
}

void tm_sub(const struct timespec *tp_s)
{
	struct timespec tp_e = {0};

	if (clock_gettime(CLOCK_MONOTONIC, &tp_e) != 0)
	{
		perror("clock_gettime tm_end failed");
		exit(-1);
	}

	long off = tp_e.tv_sec * 1000000000 + tp_e.tv_nsec - tp_s->tv_sec * 1000000000 - tp_s->tv_nsec;
	long sec = off / 1000000000;
	long nsec = off % 1000000000;

	printf("use %ld s, %ld ns\n", sec, nsec);
}